<?php

namespace app\admin\controller\content;

use app\admin\controller\Controller;
use app\admin\model\article\Article as ArticleModel;
use app\admin\model\article\Category as CategoryModel;
use think\Db;

/**
 * 房源控制器
 */
class Article extends Controller
{

    public function index(){
        $model = new ArticleModel;
        $list = $model->getList();
        return $this->fetch('index', compact('list'));
    }

    /**
     * 添加房源
     */
    public function add(){
        $model = new ArticleModel;
        if(empty($this->postData('goods')['name'])){
            //房源分类
            $catgory = CategoryModel::getAll();
            return $this->fetch('add', compact('catgory'));
        }
        //新增房源
        $data=$this->postData('goods');
        $data['loupanimageids']=json_encode($data['loupanimageids']);
        $data['shapanimageids']=json_encode($data['shapanimageids']);
        $data['peitaoimageids']=json_encode($data['peitaoimageids']);
        $data['slimageids']=json_encode($data['slimageids']);
        $data['bannerids']=json_encode($data['bannerids']);
        $insert=Db::name('yonghui_building')->insertGetId($data);
        if($insert){
            return $this->success('添加成功', url('content.article/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 更新房源信息
     */
    public function edit(){
        //房源详情
        $model = ArticleModel::detail();
        if (!$this->request->isAjax()){
            //房源分类
            $catgory=CategoryModel::getAll();
            return $this->fetch('edit', compact('model', 'catgory'));
        }
        //更新房源信息
        if ($model->edit($this->postData('goods'))) {
            return $this->renderSuccess('更新成功', url('content.article/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除房源
     */
    public function delete(){
        //房源详情
        $model = ArticleModel::detail();
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
}